import alias from '@rollup/plugin-alias'
import babel from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import typescript from '@rollup/plugin-typescript'
import { resolve } from 'path'
import type { RollupOptions } from 'rollup'
import { defineConfig } from 'rollup'
import del from 'rollup-plugin-delete'
import { dts } from 'rollup-plugin-dts'
import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))

export default [
    defineConfig({
        input: {
            index: './src/core/index.ts',
            main: './src/index.ts'
        },
        output: [
            {
                dir: './bin',
                exports: 'named',
                entryFileNames: '[name].cjs',
                chunkFileNames: 'common/[name].cjs',
                format: 'cjs',
                banner: (chunInfo) => {
                    return ['main.cjs'].includes(chunInfo.fileName) ? '#! /usr/bin/env node\n' : ''
                }
            },
            {
                dir: './bin',
                entryFileNames: '[name].mjs',
                chunkFileNames: 'common/[name].mjs',
                exports: 'named',
                format: 'esm',
                banner: (chunInfo) => {
                    return ['main.mjs'].includes(chunInfo.fileName) ? '#! /usr/bin/env node\n' : ''
                }
            }
        ],
        cache: true,
        external: [/^@swc/, 'rollup'],
        plugins: [
            alias({
                entries: [{ find: '@', replacement: resolve(__dirname, './src') }]
            }),
            replace({
                preventAssignment: true
            }),
            babel({
                babelrc: false,
                include: ['./src/**/*.ts'],
                extensions: ['.js', '.ts'],
                babelHelpers: 'bundled',
                exclude: 'node_modules/**',
                presets: [['@babel/preset-env'], '@babel/preset-typescript']
            }),
            nodeResolve({
                exportConditions: ['node', 'import', 'require', 'default'],
                browser: false,
                preferBuiltins: true
            }),
            commonjs({
                transformMixedEsModules: true,
                include: [/node_modules/]
            }),

            typescript({
                tsconfig: './tsconfig.json',
                exclude: ['node_modules']
            }),
            json()
        ],
        onwarn: (warning, warn) => {
            if (warning.message.includes('Circular dependency') || warning.code === 'EVAL') {
                return
            }
            warn(warning)
        }
    }),
    defineConfig({
        input: './src/core/index.ts',
        output: [
            {
                file: './bin/index.d.ts',
                format: 'es'
            }
        ],
        plugins: [
            alias({
                entries: [{ find: '@', replacement: resolve(__dirname, './src') }]
            }),
            dts(),
            process.env.NODE_ENV === 'production'
                ? del({ hook: 'buildEnd', targets: './bin/types' })
                : []
        ]
    })
] as RollupOptions[]
